package com.wiley.loginPO;

import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import io.appium.java_client.android.AndroidDriver;

public class LoginWithWeixinPage extends BasePage {
	
	LoggerControler log;
	/**
	 * 页面元素
	 */
	//我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	//点击登录按钮
	public static final By login_tv_loc = By.id(BaseId + "login_tv");

	//社交账号：微信
	public static final By login_with_weixin_loc = By.id(BaseId + "login_with_wx");
	
	//微信账号
	public static final By weixin_account_loc = By.name("QQ号/微信号/Email");
	
	//密码
	public static final By weixin_pwd_loc = By.xpath("//android.widget.EditText[@NAF = 'true']");
	
	//登录
	public static final By weixin_submit_loc = By.name("登录");
	
	//用户昵称
	public static final By nick_tv_loc = By.id(BaseId + "nick_tv");

	/**
	 * 操作方法
	 */
	//点击我的
	public void clickMineItem(){
		this.clickEle(mine_item_loc);
	}

	//登录按钮
	public void clickLoginButton(){
		this.clickEle(login_tv_loc);
	}

	//点击微信账号
	public void clickLoginWithWeixin(){
		this.clickEle(login_with_weixin_loc);
	}
	
	//微信登录
	public void loginWithWx() throws InterruptedException{
		try{
			if(super.isElementPresent(By.name("微信登录"))){
				driver.tap(1, 338, 683, 500);
				Assert.assertTrue(getUserName().equals("洛牧决6"));
			}else{
				this.findEle(weixin_account_loc).sendKeys("18250862110");
				this.findEle(weixin_pwd_loc).sendKeys("fgwvip123");
				this.clickEle(weixin_submit_loc);
				Thread.sleep(15000);
				driver.tap(1, 338, 683, 500); //坐标位置
				Assert.assertTrue(getUserName().equals("洛牧决6"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//用户昵称
	public String getUserName(){
		WebElement ele = this.findEle(nick_tv_loc);
		return ele.getText();
	}

	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 * @throws InterruptedException 
	 */
	//登录
	public void LoginWithWeixin(Class name) throws MalformedURLException, InterruptedException  {
		log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickMineItem();
		this.clickLoginButton();
		this.clickLoginWithWeixin();
		this.loginWithWx();
		log.info("该case测试通过！");
	}
}
